<template>
  <div class="sign-up-info">
    <div class="search-content">
      <div class="time-range-wrapper">
        <span class="time-range-label">下单时间：</span>
        <el-date-picker
          v-model="addTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <div class="time-range-wrapper">
        <span class="span-title">支付时间：</span>
        <el-date-picker
          v-model="payTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <span class="span-title">会员姓名：</span>
      <el-input
        class="search-content-input"
        v-model="searchContent.member_name"
        placeholder="会员姓名"
      ></el-input>
      <span class="span-title">会员手机号：</span>
      <el-input
        class="search-content-input"
        v-model="searchContent.member_mobile"
        placeholder="会员手机号"
      ></el-input>
      <span class="span-title">店铺名称：</span>
      <el-input
        class="search-content-input"
        v-model="searchContent.store_name"
        placeholder="店铺名称"
      ></el-input>
      <span class="span-title">订单状态：</span>
      <el-select v-model="searchContent.pay_status" placeholder="订单状态">
        <el-option
          v-for="item in statusList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <span class="span-title">支付渠道：</span>
      <el-select v-model="searchContent.pay_channel" placeholder="支付渠道">
        <el-option
          v-for="item in channelList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <span class="span-title">订单号：</span>
      <el-input
        class="search-content-input"
        v-model="searchContent.pay_ordersn"
        placeholder="订单号"
      ></el-input>
      <span class="span-title">有效性：</span>
      <el-select v-model="searchContent.enabled" placeholder="有效性">
        <el-option
          v-for="item in enabledList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <span class="span-title">支付分类：</span>
      <el-select v-model="searchContent.channels" placeholder="支付分类">
        <el-option
          v-for="item in channelsList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <span class="span-title">订单来源：</span>
      <el-select v-model="searchContent.source" placeholder="订单来源">
        <el-option
          v-for="item in sourceList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <!-- <span class="span-title">红包主题：</span>
      <el-input
        class="search-content-input"
        v-model="searchContent.car_theme_name"
        placeholder="红包主题"
      ></el-input> -->
      <span class="span-title">对账状态：</span>
      <el-select
        clearable
        v-model="searchContent.reconciliation_status"
        placeholder="对账状态"
      >
        <el-option value="points_yes" label="积分已对账"></el-option>
        <el-option value="points_no" label="积分未对账"></el-option>
        <el-option value="hb_yes" label="红包已对账"></el-option>
        <el-option value="hb_no" label="红包未对账"></el-option>
      </el-select>
  	<el-checkbox v-model="searchContent.hb_money" :true-label="1" :false-label="0">红包抵扣</el-checkbox>
			<el-checkbox v-model="searchContent.pay_points" :true-label="1" :false-label="0">积分抵扣</el-checkbox>
      <el-button type="primary" @click="search" style="margin-left: 10px">搜索</el-button>
      <el-button type="primary" @click="resetSearchData">重置</el-button>
    </div>
    <div class="search-list">
      <el-table
        :data="couponList"
        border
        :height="oHeight"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column
          align="center"
          prop="pay_id"
          label="编号"
          min-width="60"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="pay_ordersn"
          label="订单号"
          min-width="160"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="store_name"
          label="店铺"
          min-width="120"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="member_name"
          label="用户账号"
          min-width="120"
        ></el-table-column>
        <el-table-column
          align="center"
          label="手机号	"
          prop="member_mobile"
          min-width="120"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="pay_want_money"
          label="消费金额	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="discount_money"
          label="优惠金额	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          prop="pay_points"
          align="center"
          label="积分抵扣	"
          min-width="120"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="hb_money"
          label="红包抵扣	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="points_reconciliation_desc"
          label="积分对账	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="hb_reconciliation_desc"
          label="红包对账	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="other_money"
          label="其他金额	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="pay_money"
          label="实际付款	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          label="时间"
          prop="add_time"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="note"
          label="备注信息	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="channels_desc"
          label="支付分类	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          label="订单状态"
          prop="pay_status_desc"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="bytc_desc"
          label="保养套餐状态	"
          min-width="100"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="source_desc"
          label="来源	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="enabled_desc"
          label="是否有效	"
          min-width="90"
        ></el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          label="操作"
          min-width="350"
          default="{ row }"
        >
        <template slot-scope="scope">
          <el-button
            v-if="(scope.row.pay_status==20||scope.row.pay_status==30)&&scope.row.points_reconciliation_status == '0' && scope.row.pay_points && Number(scope.row.pay_points) > 1"
            type="primary"
            @click="JFCheak(scope.row)"
            >积分对账</el-button
          >
          <el-button
            v-else-if="scope.row.points_reconciliation_status === '1'"
            type="primary"
            @click="JFCheakBack(scope.row)"
            >积分对账撤回</el-button
          >
          <el-button
            v-if="(scope.row.pay_status==20||scope.row.pay_status==30)&&scope.row.hb_reconciliation_status == '0'&& scope.row.hb_money && Number(scope.row.hb_money)>1"
            type="primary"
            @click="HBCheak(scope.row)"
            >红包对账</el-button
          >
          <el-button
            v-else-if="scope.row.hb_reconciliation_status === '1'"
            type="primary"
            @click="HBCheakBack(scope.row)"
            >红包对账撤回</el-button
          >
          <!-- <el-button
            v-if="row.is_bill === '1' && row.is_review === '0'"
            type="primary"
            @click="billRollback(row)"
            >对账撤回</el-button
          > -->
          <el-button
            :type="getRefundStaus(scope.row.refund_status * 1).type"
            :disabled="getRefundStaus(scope.row.refund_status * 1).disabled || parseFloat(scope.row.pay_money) <= 0"
            @click="refund(scope.row)"
          >
            {{ getRefundStaus(scope.row.refund_status * 1).text }}</el-button
          >
        </template>
          <!-- <el-button @click="refund(scope.row.id)" type="primary" size="mini">退款</el-button> -->
        </el-table-column>
      </el-table>
      <div class="bottom">
        <div class="btn-group">
          <a class="btn-bottom" style="cursor:pointer" @click="exportLink"
            >导出</a
          >
          <a
            class="btn-bottom"
            style="cursor:pointer;margin-left:10px;"
            @click="oneBillHB"
            >一键红包对账</a
          >
           <a
            class="btn-bottom"
            style="cursor:pointer;margin-left:10px;"
            @click="oneBillJF"
            >一键积分对账</a
          >
       
        </div>
        <div class="paginationOne">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="searchContent.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="searchContent.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { resize } from "@/common/js/height.js";
import Qs from "qs";
import { dateFormatter } from "../../../../common/js/util";
export default {
  data() {
    return {
      couponList: [],
      statusList: [], //订单状态
      channelsList: [], //支付分类
      enabledList: [], //有效无效
      channelList: [], //支付渠道
      sourceList: [], //订单来源
      oHeight: 800,
      addTime: "",
      payTime: "",
      searchContent: {
        add_time_start: "",
        add_time_end: "",
        pay_time_start: "",
        pay_time_end: "",
        project_class: "",
        member_name: "",
        member_mobile: "",
        store_name: "",
        pay_status: "",
        pay_channel: "",
        pay_ordersn: "",
        enabled: "",
        channels: "",
        source: "",
        reconciliation_status:"",
        page: 1,
        discount_money:0,
				pay_points:0,
        pageSize: 10,
        form_submit: "",
        is_bill: "",
        // hb_reconciliation_status:0,
        // points_reconciliation_status:0,
        hb_reconciliation_desc:"",
        points_reconciliation_desc:""
      },
      total: 0,
      check: [],
    };
  },
  created() {
    this.getListData();
    this.getstatus();
    this.getchannels();
    this.getenabled();
    this.getchannel();
    this.getsource();
  },
  computed: {},
  methods: {
    oneBillHB(type) {
      let url = "/admin/bill/index.php?act=onlineBill&op=reconcileOnlineOrder";
      let idArr = [];
      let dataErr = false;
      if (this.check.length === 0) {
        this.$message.warning("请勾选数据");
        return;
      }
      this.check.forEach((item) => {
        if (
          
          (item.pay_status==20||item.pay_status==30)&&item.hb_reconciliation_status !== "1"&&item.hb_money && Number(item.hb_money)>1
        ) {
          idArr.push(item.pay_id);
        } else {
          dataErr = true;
        }
      });
      if (dataErr) {
        this.$message.error("请选择正确的状态！");
        return;
      }
     this.$confirm("确认").then((res) => {
      this.$http
        .post(url, {
          pay_id: idArr,
         reconcile_type: "hb",
        })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.search();
             this.getListData();
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
     });
    },
     oneBillJF(type) {
      let url = "/admin/bill/index.php?act=onlineBill&op=reconcileOnlineOrder";
      let idArr = [];
      let dataErr = false;
      if (this.check.length === 0) {
        this.$message.warning("请勾选数据");
        return;
      }
      this.check.forEach((item) => {
        if (
          
           (item.pay_status==20||item.pay_status==30)&&item.points_reconciliation_status === "0"&&item.pay_points && Number(item.pay_points)>1
        
        ) {
          idArr.push(item.pay_id);
        } else {
          dataErr = true;
        }
      });
      if (dataErr) {
        this.$message.error("请选择正确的状态！");
        return;
      }
      if (type === 1) {
        url = "/admin/bill/index.php?act=onlineBill&op=billRollback";
      }
      this.$confirm("确认").then((res) => {
      this.$http
        .post(url, {
          pay_id: idArr,
          reconcile_type: "points",
        })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.search();
             this.getListData();
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
       });
    },
   
    refund(item) {
      let that = this;
      this.$confirm("确认退款").then((res) => {
        this.$http
          .post("/admin/bill/index.php?act=commonRefund&op=refund", {
            pay_ordersn: item.pay_ordersn,
          })
          .then((res) => {
            if (res.code == 1) {
              that.getListData(); //刷新列表
            } else {
              alert(res.msg);
            }
          });
      });
    },
    handleSelectionChange(val) {
      this.check = val;
    },
    //订单状态获取
    getstatus() {
      this.$http
        .post("/admin/onlinePay/index.php?act=order&op=getPayStatus")
        .then((res) => {
          if (res.code == 1) {
            this.statusList = res.data;
          }
        });
    },

    getRefundStaus(staus) {
      let type = "info";
      let text = "退款";
      let disabled = true;
      switch (staus) {
        case 0:
          type = "primary";
          text = "退款";
          disabled = false;
          break;
        case 10:
          type = "info";
          text = "退款中";
          disabled = true;
          break;
        case 20:
          type = "info";
          text = "已退款";
          disabled = true;
          break;
        case 30:
          type = "info";
          text = "退款失败";
          disabled = true;
          break;
      }
      let a = {
        type,
        text,
        disabled,
      };
      return a;
    },
    //支付分类获取
    getchannels() {
      this.$http
        .post("/admin/bill/index.php?act=reconciliation&op=getBusinessTypes")
        .then((res) => {
          if (res.code == 1) {
            this.channelsList = res.data;
          }
        });
    },
    //有效无效获取
    getenabled() {
      this.$http
        .post("/admin/onlinePay/index.php?act=order&op=getEnabled")
        .then((res) => {
          if (res.code == 1) {
            this.enabledList = res.data;
          }
        });
    },
    //支付渠道获取
    getchannel() {
      this.$http
        .post("/admin/onlinePay/index.php?act=order&op=getPayChannels")
        .then((res) => {
          if (res.code == 1) {
            this.channelList = res.data;
          }
        });
    },
    //订单来源获取
    getsource() {
      this.$http
        .post("/admin/onlinePay/index.php?act=order&op=getPaySources")
        .then((res) => {
          if (res.code == 1) {
            this.sourceList = res.data;
          }
        });
    },
    search() {
      this.searchContent.page = 1;
      this.getListData();
    },
    resetSearchData() {
      this.addTime = "";
      this.payTime = "";
      this.reconciliation_status="";
    
      this.searchContent = {
        page: 1,
        pageSize: localStorage.getItem("pageSize") * 1 || 10,
        pay_points:0,
				hb_money:0
      };
      this.getListData();
    },
    handleSizeChange(pageSize) {
      this.searchContent.pageSize = pageSize;
      this.searchContent.page = 1;
      this.getListData();
    },
    handleCurrentChange(page) {
      this.searchContent.page = page;
      this.getListData();
    },
    getListData() {
      const data = this.searchContent;
      data.is_export = "";
      if (this.addTime) {
        const startAddMS = this.addTime[0].getTime();
        const endAddMS = this.addTime[1].getTime();
        if (startAddMS == endAddMS) {
          data.add_time_start = dateFormatter("YYYY-MM-DD HH:mm:ss", startAddMS);
          data.add_time_end = dateFormatter(
            "YYYY-MM-DD HH:mm:ss",
            endAddMS + (24 * 60 * 60 - 1) * 1000
          );
        } else {
          data.add_time_start = dateFormatter("YYYY-MM-DD HH:mm:ss", this.addTime[0]);
          data.add_time_end = dateFormatter("YYYY-MM-DD HH:mm:ss", endAddMS + (24 * 60 * 60 - 1) * 1000);
        }
      }
      if (this.payTime) {
        const startPAYMS = this.payTime[0].getTime();
        const endPAYMS = this.payTime[1].getTime();
        if (startPAYMS == endPAYMS) {
          data.pay_time_start = dateFormatter("YYYY-MM-DD HH:mm:ss", startPAYMS);
          data.pay_time_end = dateFormatter(
            "YYYY-MM-DD HH:mm:ss",
            endPAYMS + (24 * 60 * 60 - 1) * 1000
          );
        } else {
          data.pay_time_start = dateFormatter("YYYY-MM-DD HH:mm:ss", this.payTime[0]);
          data.pay_time_end = dateFormatter("YYYY-MM-DD HH:mm:ss", endPAYMS + (24 * 60 * 60 - 1) * 1000);
        }
      }
      this.$http
        .post("/admin/onlinePay/index.php?act=order&op=payedList", data)
        .then((res) => {
          if (res.code == 1) {
            if (res.data && res.data.list) {
              this.couponList = res.data.list.map(function(item) {
                return {
                  pay_id: item.pay_id,
                  pay_ordersn: item.pay_ordersn,
                  add_time: item.add_time,
                  pay_want_money: item.pay_want_money,
                  pay_money: item.pay_money,
                  discount_money: item.discount_money,
                  pay_points: item.pay_points,
                  hb_money: item.hb_money,
                  note: item.note,
                  other_money: item.other_money,
                  pay_status: item.pay_status,
                  pay_status_desc: item.pay_status_desc,
                  pay_channel_desc: item.pay_channel_desc,
                  channels_desc: item.channels_desc,
                  source_desc: item.source_desc,
                  enabled_desc: item.enabled_desc,
                  store_name: item.store_name,
                  member_name: item.member_name,
                  member_mobile: item.member_mobile,
                  bytc_desc: item.bytc_desc,
                  is_bill: item.is_bill,
                  is_review: item.is_review,
                  refund_status: item.refund_status * 1,
                  hb_reconciliation_desc:item.hb_reconciliation_desc,
                  points_reconciliation_desc:item.points_reconciliation_desc,
                  reconciliation_status:item.reconciliation_status,
                  points_reconciliation_status:item.points_reconciliation_status,
                  hb_reconciliation_status:item.hb_reconciliation_status
                };
              });
              this.total = parseInt(res.data.totalCount);
              // this.channelList = res.data.class_list;
            } else {
              this.couponList = [];
              this.total = 0;
            }
          } else if (res.code == 2001) {
            this.couponList = [];
            this.total = 0;
          } else {
            alert(res.msg);
          }
        });
    },
    dateFormat(row) {
      console.log("888888", row);
      var timestamp = row;
      var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      console.log("1111", timestamp);
      var Y = date.getFullYear() + "-";
      var M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D = date.getDate() + " ";
      return Y + M + D;
    },
    exportLink() {
      const data = this.searchContent;
      data.is_export = 1;
      if (this.addTime) {
        const startAddMS = this.addTime[0].getTime();
        const endAddMS = this.addTime[1].getTime();
        if (startAddMS == endAddMS) {
          data.add_time_start = dateFormatter("YYYY-MM-DD HH:mm:ss", startAddMS);
          data.add_time_end = dateFormatter(
            "YYYY-MM-DD HH:mm:ss",
            endAddMS + (24 * 60 * 60 - 1) * 1000
          );
        } else {
          data.add_time_start = dateFormatter("YYYY-MM-DD HH:mm:ss", this.addTime[0]);
          data.add_time_end = dateFormatter("YYYY-MM-DD HH:mm:ss", endAddMS + (24 * 60 * 60 - 1) * 1000);
        }
      }
      if (this.payTime) {
        const startPAYMS = this.payTime[0].getTime();
        const endPAYMS = this.payTime[1].getTime();
        if (startPAYMS == endPAYMS) {
          data.pay_time_start = dateFormatter("YYYY-MM-DD HH:mm:ss", startPAYMS);
          data.pay_time_end = dateFormatter(
            "YYYY-MM-DD HH:mm:ss",
            endPAYMS + (24 * 60 * 60 - 1) * 1000
          );
        } else {
          data.pay_time_start = dateFormatter("YYYY-MM-DD HH:mm:ss", this.payTime[0]);
          data.pay_time_end = dateFormatter("YYYY-MM-DD HH:mm:ss", endPAYMS + (24 * 60 * 60 - 1) * 1000);
        }
      }
      let domain = document.domain;
      var url =
        location.protocol +
        "//" +
        domain.replace("admin", "api") +
        "/admin/onlinePay/index.php?act=order&op=payedList";
      console.log(url);
      for (var key in data) {
        if (
          !(data[key] === null || data[key] === "" || data[key] === undefined)
        ) {
          url += "&" + key + "=" + data[key];
        }
      }
      console.log(url);
      window.location.href = url;
    },
//单个红包对账
      HBCheak(row) {
         this.$confirm("确认").then((res) => {
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=reconcileOnlineOrder", {
          pay_id: row.pay_id,
          reconcile_type: "hb",
        })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.getListData();
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
     });
    },
     HBCheakBack(row) {
       this.$confirm("确认").then((res) => {
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=onlineOrderReconciliationWithdraw", {
          pay_id: row.pay_id,
          withdraw_type: "hb",
        })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
             this.getListData();
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });

       }); 
    },
    
    //单个积分对账
      JFCheak(row) {

         this.$confirm("确认").then((res) => {
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=reconcileOnlineOrder", {
          pay_id: row.pay_id,
           reconcile_type: "points",
        })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
             this.getListData();
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
      });
    },
     JFCheakBack(row) {

        this.$confirm("确认").then((res) => {
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=onlineOrderReconciliationWithdraw", {
          pay_id: row.pay_id,
          withdraw_type: "points",
        })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
             this.getListData();
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });

     });
    },
    
    
    bill(row) {
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=bill", {
          id: row.pay_id,
          order_type: "2",
        })
        .then((res) => {
          if (res.code === 1) {
            row.is_bill = "1";
            this.$message.success(res.msg);
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    // billRollback(row) {
    //   this.$http
    //     .post("/admin/bill/index.php?act=onlineBill&op=billRollback", {
    //       id: row.pay_id,
    //       order_type: "2",
    //     })
    //     .then((res) => {
    //       if (res.code === 1) {
    //         row.is_bill = "0";
    //         this.$message.success(res.msg);
    //       } else {
    //         this.$message.warning(res.msg);
    //       }
    //     })
    //     .catch((err) => {
    //       this.$message.error(err);
    //     });
    // },
  },
  mounted() {
    resize(this);
  },
};
</script>

<style lang="scss" scoped>
.sign-up-info {
  .search-content {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    &-input {
      width: 200px;
    }
    .time-range-wrapper {
      .time-range-label {
        font-size: 14px;
      }
      display: flex;
      align-items: center;
      /deep/.el-range-separator {
        box-sizing: content-box;
      }
    }
  }
  .bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
.btn-bottom {
  background: #409eff;
  color: #fff;
  border: 0;
  font-size: 14px;
  padding: 5px 8px;
  border-radius: 3px;
  display: inline-block;
}
.span-title {
  font-size: 14px;
  color: rgb(46, 45, 45);
}
</style>
